'use strict';
const db = uniCloud.database()
const weightfc = db.collection('weight_records')
const user = db.collection('users')
const userinfo = db.collection('userinfo')
const {
	verifyToken
} = require('wx-common')

exports.main = async (event, context) => {
	const {
		action,
		data
	} = event

	switch (action) {
		case 'add':
			return await addWeight(event)
		case 'getList':
			return await getWeightList(event)
		case 'getLatest':
			return await getLatestWeight(event)
		case 'getStats':
			return await getWeightStats(event)
		case 'updateTarget':
			return await updateTarget(event)
		case 'updateInitial':
			return await updateInitial(event)
		case 'updateHeight':
			return await updateHeight(event)
		case 'getWeightListByDate':
			return await getWeightListByDate(event)
		case 'getuserWeight':
			return await getuserWeight(event)
		case 'getWeightListByuser':
			return await getWeightListByuser(event)
		default:
			return {
				code: -1,
					msg: '未知操作'
			}
	}
}

// 添加体重记录
async function addWeight(event) {
	try {
		const {
			token,
			data
		} = event

		if (!token) {
			return {
				code: -1,
				msg: '未登录'
			}
		}

		// 验证token
		const decoded = verifyToken(token)
		if (!decoded) {
			return {
				code: -1,
				msg: 'token无效'
			}
		}

		const {
			weight,
			image,
			note
		} = data
		// 添加记录
		weightfc.add({
			openid: decoded.value,
			weight,
			date: new Date(),
			image,
			note
		})
		return {
			code: 0,
			msg: '添加成功'
		}
	} catch (e) {
		return {
			code: -1,
			msg: e.message
		}
	}
}
// 获取指定时间范围内的记录列表
async function getWeightListByDate(event) {
	try {
		const {
			token,
			data
		} = event
		if (!token) {
			return {
				code: -1,
				msg: '未登录'
			}
		}
		const dbCmd = db.command
		// 验证token
		const decoded = verifyToken(token)
		if (!decoded) {
			return {
				code: -1,
				msg: 'token无效'
			}
		}
		const res = await weightfc.where({
			openid: decoded.value,
			date: dbCmd.gt(data.startTime).and(dbCmd.lt(data.endTime))
		}).get()

		return {
			code: 0,
			msg: 'success',
			data: res.data
		}
	} catch (e) {
		return {
			code: -1,
			msg: e.message
		}
	}
}
// 获取某用户的记录
async function getWeightListByuser(event) {
	try {
		const {
			token,
			data
		} = event

		if (!token) {
			return {
				code: -1,
				msg: '未登录'
			}
		}

		// 验证token
		const decoded = verifyToken(token)
		if (!decoded) {
			return {
				code: -1,
				msg: 'token无效'
			}
		}
		const res= await weightfc.where({
				openid:data.openid
			})
			.orderBy('date', 'desc')
			.get()

		return {
			code: 0,
			msg: 'success',
			data:res.data
		}
	} catch (e) {
		return {
			code: -1,
			msg: e.message
		}
	}
}
// 获取指定日期打卡人
async function getuserWeight(event) {
	try {
		const {
			token,
			data
		} = event

		if (!token) {
			return {
				code: -1,
				msg: '未登录'
			}
		}

		// 验证token
		const decoded = verifyToken(token)
		if (!decoded) {
			return {
				code: -1,
				msg: 'token无效'
			}
		}
		const dbCmd = db.command
		const $ = db.command.aggregate;
		const res = await weightfc.aggregate().match({
				date: dbCmd.gt(data.startTime).and(dbCmd.lt(data.endTime))
			}).lookup({
				from: 'user',
				localField: 'openid',
				foreignField: 'openid',
				as: 'userInfo'
			}).unwind('$userInfo').group({
				_id: '$openid', // 
				userData: $.first('$userInfo'),
				records: $.push({ // 
					weight: '$weight',
					date: '$date',
					image: '$image'
				})
			})
			.project({
				_id: 0, // 隐藏_id字段
				userData: 1,
				records: 1
			})
			.end();

		return {
			code: 0,
			msg: 'success',
			data: res.data
		}
	} catch (e) {
		return {
			code: -1,
			msg: e.message
		}
	}
}

// 获取体重记录列表
async function getWeightList(event) {
	try {
		const {
			token
		} = event

		if (!token) {
			return {
				code: -1,
				msg: '未登录'
			}
		}

		// 验证token
		const decoded = verifyToken(token)
		if (!decoded) {
			return {
				code: -1,
				msg: 'token无效'
			}
		}
		const {
			data
		} = await weightfc.where({
				openid: decoded.value,
			})
			.orderBy('date', 'desc')
			.get()

		return {
			code: 0,
			msg: 'success',
			data
		}
	} catch (e) {
		return {
			code: -1,
			msg: e.message
		}
	}
}
// 获取最新体重记录
async function getLatestWeight(event) {
	try {
		const {
			token
		} = event

		if (!token) {
			return {
				code: -1,
				msg: '未登录'
			}
		}

		// 验证token
		const decoded = verifyToken(token)
		if (!decoded) {
			return {
				code: -1,
				msg: 'token无效'
			}
		}

		const {
			data
		} = await weightfc.where({
				oopenid: decoded.value,
			})
			.orderBy('date', 'desc')
			.limit(1)
			.get()

		return {
			code: 0,
			msg: 'success',
			data
		}
	} catch (e) {
		return {
			code: -1,
			msg: e.message
		}
	}
}
// 获取体重统计信息
async function getWeightStats(event) {
	try {
		const {
			token
		} = event

		if (!token) {
			return {
				code: -1,
				msg: '未登录'
			}
		}

		// 验证token
		const decoded = verifyToken(token)
		if (!decoded) {
			return {
				code: -1,
				msg: 'token无效'
			}
		}

		// 获取所有记录
		const {
			data
		} = await userinfo.where({
			opopenid: decoded.value,
		}).get()
		if (data.length === 0) {
			return {
				code: 0,
				msg: 'success',
				data
			}
		}

		// 计算统计数据
		const initialWeight = data[0].weight
		const currentWeight = data[data.length - 1].weight
		const targetWeight = 60 // 目标体重，可以根据需要修改
		return {
			code: 0,
			msg: 'success',
			data
		}
	} catch (e) {
		return {
			code: -1,
			msg: e.message
		}
	}
}